package com.liutaw.nanjingair.views.viewcomponent.weather;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.liutaw.nanjingair.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 天气趋势折线图
 *
 * @author Dave
 */
public class TrendView extends View {

    private Paint mPointPaint;        //点     画笔
    private Paint mTextPaint;        //度数    画笔
    private Paint mTitlePaint;        //标题“趋势” 画笔
    private Paint mTimePaint;        //时间    画笔
    private Paint mLinePaint1;        //上线    画笔
    private Paint mLinePaint2;        //下线    画笔

    private int x[] = new int[6];    //横坐标   6个点的位置
    private float radius = 8;        //点    的半径
    private int h;                    //纵坐标
    private List<Integer> topTem;    //最高温度的集合
    private List<Integer> lowTem;    //最低温度的集合
    private List<String> timeWeak;    //周 的集合
    private List<String> timeDate;    //日期的集合
    private Bitmap[] topBmps;        //最高温度的图片
//	private Bitmap[] lowBmps;

    private Context c;

    public TrendView(Context context) {
        super(context);
        this.c = context;
        init();
    }

    public TrendView(Context context, AttributeSet attr) {
        super(context, attr);
        this.c = context;
        init();
    }

    private void init() {
        topBmps = new Bitmap[6];
//		lowBmps = new Bitmap[6];

        topTem = new ArrayList<Integer>();
        lowTem = new ArrayList<Integer>();
        timeWeak = new ArrayList<String>();
        timeDate = new ArrayList<String>();
        //初始化各个画笔
        mPointPaint = new Paint();
        mPointPaint.setAntiAlias(true);
        mPointPaint.setColor(Color.WHITE);

        mLinePaint1 = new Paint();
        mLinePaint1.setColor(Color.YELLOW);
        mLinePaint1.setAntiAlias(true);
        mLinePaint1.setStrokeWidth(4);
        mLinePaint1.setStyle(Style.FILL);

        mLinePaint2 = new Paint();
        mLinePaint2.setColor(Color.BLUE);
        mLinePaint2.setAntiAlias(true);
        mLinePaint2.setStrokeWidth(4);
        mLinePaint2.setStyle(Style.FILL);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(25F);
        mTextPaint.setTextAlign(Align.CENTER);

        mTitlePaint = new Paint();
        mTitlePaint.setAntiAlias(true);
        mTitlePaint.setColor(Color.WHITE);
        mTitlePaint.setTextSize(30F);
        mTitlePaint.setTextAlign(Align.CENTER);

        mTimePaint = new Paint();
        mTimePaint.setAntiAlias(true);
        mTimePaint.setColor(Color.WHITE);
        mTimePaint.setTextSize(20F);
        mTimePaint.setTextAlign(Align.CENTER);
    }

    //设置每个点的横坐标，等分12分，以及总的纵坐标
    public void setWidthHeight(int w, int h) {
        x[0] = w / 12;
        x[1] = w * 3 / 12;
        x[2] = w * 5 / 12;
        x[3] = w * 7 / 12;
        x[4] = w * 9 / 12;
        x[5] = w * 11 / 12;

        this.h = h;
    }

    //设置温度
    public void setTemperature(List<Integer> top, List<Integer> low) {
        this.topTem = top;
        this.lowTem = low;

        postInvalidate();
    }

    //设置时间
    public void setTime(List<String> timeWeak2, List<String> timeDate2) {
        this.timeWeak = timeWeak2;
        this.timeDate = timeDate2;

        postInvalidate();
    }

    //设置图片
    public void setBitmap(List<Integer> topList) {
        for (int i = 0; i < topList.size(); i++) {
            topBmps[i] = WeatherPic.getSmallPic(c, topList.get(i), 0);
//			lowBmps[i] = WeatherPic.getSmallPic(c, lowList.get(i), 1);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float space = 0f;
        float space1 = 0f;
        int temspace = 12;
        int picSize = (int) c.getResources().getDimension(R.dimen.picSize);    //图片的高
        FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        // 计算文字高度
        float fontHeight = fontMetrics.bottom - fontMetrics.top;

        int h = this.h / 2;
        int h2 = (int) (h - fontHeight / 2);            //度数的纵坐标（高温）
        int h3 = (int) (h - fontHeight - picSize);    //图片的纵坐标（高温）

        int h4 = (int) (h + fontHeight);            //度数的纵坐标（低温）
//		int h5 = (int) (h + fontHeight);			//图片的纵坐标（低温）
        int topY = h3;        //记录最高温度的纵坐标
        for (int i = 0; i < topTem.size(); i++) {
            space = (-topTem.get(i)) * temspace;
            if (topTem.get(i) != 100) {
                if (i != topTem.size() - 1) {
                    //画5条线
                    space1 = (-topTem.get(i + 1)) * temspace;
                    canvas.drawLine(x[i], h + space, x[i + 1], h + space1, mLinePaint1);
                }
                canvas.drawText(topTem.get(i) + "°", x[i], h2 + space, mTextPaint);
                canvas.drawCircle(x[i], h + space, radius, mPointPaint);
                canvas.drawBitmap(topBmps[i], x[i] - topBmps[i].getWidth() / 2, h3 + space, null);
            }
            if (topY > h3 + space) {
                topY = (int) (h3 + space);
            }
        }
        int lowY = h4;        //记录最低温度的纵坐标
        for (int i = 0; i < lowTem.size(); i++) {
            space = (-lowTem.get(i)) * temspace;
            if (i != lowTem.size() - 1) {
                space1 = (-lowTem.get(i + 1)) * temspace;
                canvas.drawLine(x[i], h + space, x[i + 1], h + space1, mLinePaint2);
            }
            canvas.drawText(lowTem.get(i) + "°", x[i], h4 + space, mTextPaint);
            canvas.drawCircle(x[i], h + space, radius, mPointPaint);
//			canvas.drawBitmap(lowBmps[i], x[i]-lowBmps[i].getWidth()/2, h5 + space, null);
            if (lowY < (int) (h4 + space + fontHeight)) {
                lowY = (int) (h4 + space + fontHeight);
            }
        }
        topY = (int) (topY + space);
        lowY = (int) (lowY - space);
        canvas.drawText("一周天气趋势", x[0], topY, mTitlePaint);
        for (int i = 0; i < timeWeak.size(); i++) {
            canvas.drawText(timeWeak.get(i), x[i], lowY, mTimePaint);
            canvas.drawText(timeDate.get(i), x[i], lowY + fontHeight, mTimePaint);
        }
    }

}